//
//  ChatVideoTCell.m
//  testVCtoVC
//
//  Created by Lenny on 16/4/23.
//  Copyright © 2016年 Lenny. All rights reserved.
//

#import "ChatVideoTCell.h"
#import "Masonry.h"

@interface ChatVideoTCell ()
{
    CGFloat _picWide;
}

@property(nonatomic,strong)UIImageView *picIV;
@property(nonatomic,strong)UIImageView *contentBackIV;

@property(nonatomic,strong)UILabel *contentL;
@property(nonatomic,strong)UIImageView *vedioI;

@property(nonatomic,strong)NSArray *conArray;

@end

@implementation ChatVideoTCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


- (void)addUI {
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    [self setData];
    [self addSubview:self.picIV];
    [self addSubview:self.contentBackIV];
    [self.contentBackIV addSubview:self.contentL];
    [self.contentBackIV addSubview:self.vedioI];
    
    __weak typeof(self) weakSelf = self;
    [self.picIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.width.mas_equalTo(_picWide);
        make.height.mas_equalTo(_picWide);
        make.right.mas_equalTo(-10);
    }];
    
    self.conArray = [self.contentBackIV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf.mas_centerY);
        make.right.equalTo(weakSelf.picIV.mas_left).offset(-10);
        make.width.mas_equalTo(200);
    }];
    
    [self.contentL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(10);
        make.bottom.mas_equalTo(-10);
    }];
    
    [self.vedioI mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf.contentL);
        make.right.equalTo(weakSelf.contentL.mas_left).offset(-5);
    }];

}

#pragma mark - public methods
- (void)addAni {
    
    NSMutableArray *arr = [NSMutableArray array];
    [arr addObject:[UIImage imageNamed:@"chat_animation_white1.png"]];
    [arr addObject:[UIImage imageNamed:@"chat_animation_white2.png"]];
    [arr addObject:[UIImage imageNamed:@"chat_animation_white3.png"]];
    self.vedioI.animationImages = arr;
    self.vedioI.animationDuration = 1.0f;
    self.vedioI.animationRepeatCount = 0;
    [self.vedioI startAnimating];
}


#pragma mark - private methods
- (void)setData {
    
    _picWide = 50;
}

#pragma mark - setters and getters

- (UIImageView *)picIV {
    
    if (_picIV == nil) {
        
        _picIV = [[UIImageView alloc] init];
        _picIV.backgroundColor = [UIColor redColor];
        _picIV.layer.cornerRadius = _picWide / 2;
    }
    return _picIV;
}

- (UIImageView *)contentBackIV {
    
    if (_contentBackIV == nil) {
        
        _contentBackIV = [[UIImageView alloc] init];
        UIImage *image = [UIImage imageNamed:@"icon_message_to"];
        _contentBackIV.image = image;
        _contentBackIV.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 20, 20)];
        _contentBackIV.userInteractionEnabled = YES;
        
        UIButton *btn = [[UIButton alloc] init];
        [_contentBackIV addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
        }];
        [btn addTarget:self action:@selector(addAni) forControlEvents:UIControlEventTouchUpInside];
    }
    return _contentBackIV;
}

- (UILabel *)contentL {
    
    if (_contentL == nil) {
        
        _contentL = [[UILabel alloc] init];
    }
    return _contentL;
}

- (UIImageView *)vedioI {
    
    if (_vedioI == nil) {
        
        _vedioI = [[UIImageView alloc] init];
        _vedioI.image = [UIImage imageNamed:@"chat_animation_white3.png"];
    }
    return _vedioI;
}


- (void)setChatModel:(ChatModel *)chatModel {
    
    _chatModel = chatModel;
    self.contentL.text = chatModel.contentText;
    
    NSString *contStr = chatModel.contentText;
    
    CGSize size = [contStr boundingRectWithSize:CGSizeMake(250, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]} context:nil].size;
    MASViewConstraint *constraint = self.conArray[2];
    constraint.with.mas_equalTo(size.width + 70);
}


@end
